<?php
/**
 *
 * 版权所有：安德玛(underArmour)
 * 作    者：1554324405@qq.com
 * 日    期：2020-05-20
 * 版    本：1.0.0
 * 功能说明：用户收款信息控制器。
 *
 **/
namespace Api\Controller;

class UserAccountController extends ComController
{   
    /**
     * 收款信息查询
     */
    public function index()
    {
        $account = M('user_payment')->where(array('uid'=>$this->user['id']))->find();
        $account['wechat_qrcode_db'] = $account['wechat_qrcode'];
        if($account['wechat_qrcode']){
            $account['wechat_qrcode'] = C('URL').$account['wechat_qrcode'];
        }
        $account['alipay_qrcode_db'] = $account['alipay_qrcode'];
        if($account['alipay_qrcode']){
            $account['alipay_qrcode'] = C('URL').$account['alipay_qrcode'];
        }
        $this->res['data'] = $account;
        $this->response($this->res,'json');
    }
    
    /**
     * 收款信息保存
     */
    public function save()
    {
        if(!IS_POST){
            $this->res['code'] = 101;
            $this->res['msg'] = '请使用post方法';
            $this->response($this->res,'json');
        }
        $post_data = file_get_contents('php://input');
        $json = json_decode($post_data);
        
        $data['kaihuhang'] = $json->kaihuhang;
        $data['zhihang'] = $json->zhihang;
        $data['real_name'] = $json->real_name;
        $data['card_no'] = $json->card_no;
        $data['wechat_no'] = $json->wechat_no;
        $data['wechat_qrcode'] = $json->wechat_qrcode;
        $data['alipay_no'] = $json->alipay_no;
        $data['alipay_qrcode'] = $json->alipay_qrcode;
        $data['type'] = $json->type;
        
        if(!$data['type']){
            $this->res['code'] = 102;
            $this->res['msg'] = '收款信息类型为空';
            $this->response($this->res,'json');
        }
        
        if($data['type'] == 1){
            if(!$data['kaihuhang'] or !$data['zhihang'] or !$data['real_name'] or !$data['card_no']){
                $this->res['code'] = 103;
                $this->res['msg'] = '银行卡信息存在空项';
                $this->response($this->res,'json');
            }
        }else if($data['type'] == 2){
            if(!$data['wechat_no'] or !$data['wechat_qrcode']){
                $this->res['code'] = 103;
                $this->res['msg'] = '微信收款信息存在空项';
                $this->response($this->res,'json');
            }
        }else{
            if(!$data['alipay_no'] or !$data['alipay_qrcode']){
                $this->res['code'] = 103;
                $this->res['msg'] = '支付宝收款信息存在空项';
                $this->response($this->res,'json');
            }
        }
        
        $num = M('user_payment')->where(array('uid'=>$this->user['id']))->count();
        if($num == 0){
            $data['t'] = time();
            $data['uid'] = $this->user['id'];
            $res = M('user_payment')->data($data)->add();
        }else{
            $data['update_t'] = time();
            $res = M('user_payment')->where(array('uid'=>$this->user['id']))->data($data)->save();
        }
        if($res){
            $this->res['msg'] = 'success';
            $this->response($this->res,'json');
        }else{
            $this->res['code'] = 500;
            $this->res['msg'] = '付款信息修改失败！';
            $this->response($this->res,'json');
        }
    }
}